package yingke.com.yingke.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import yingke.com.yingke.R;
import yingke.com.yingke.base.BaseActivity;
import yingke.com.yingke.factory.LeftAndRightFragmentFactory;
import yingke.com.yingke.ui.center.GetLiving;


public class MainActivity extends BaseActivity implements View.OnClickListener{
    private static final int LEFTFRAGMENT = 0;//左Fragment
    private static final int RIGHTFRAGMENT = 1;//右Fragment

    //当前tab的序号
    private int currentIndex = 0;
    //用于存储fragment
    private static SparseArray<Fragment> fragments=new SparseArray<>();
    private ImageView ivLeft;
    private ImageView ivRight;
    private ImageView ivCenter;

    //当前tab的序号
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    public void initView() {
        ivLeft = (ImageView) findViewById(R.id.iv_main_left);
        ivRight = (ImageView) findViewById(R.id.iv_main_right);
        ivCenter = (ImageView) findViewById(R.id.iv_main_center);
        for (int i = 0; i < 2; i++) {
            Fragment fragment = LeftAndRightFragmentFactory.createFragment(i);
            fragments.put(i,fragment);
        }
        //默认加载homefragment
        getSupportFragmentManager().beginTransaction().add(R.id.main_frame, fragments.get(0)).commit();
        ivLeft.setOnClickListener(this);
        ivCenter.setOnClickListener(this);
        ivRight.setOnClickListener(this);
    }
    //点击替换相应的fragment
    private void switchFragment(int index){
        Fragment targetFragment = fragments.get(index);
        //当前Fragment
        Fragment currentFragment = fragments.get(currentIndex);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(targetFragment.isAdded()){
            //目标Fragment已经被添加了。。。则隐藏正在展示的Fragment，显示目标的fragment
            transaction.hide(currentFragment).show(targetFragment);
        }else {
            //没有被添加...则隐藏当前的Fragment,添加目标Fragment.
            transaction.hide(currentFragment).add(R.id.main_frame,targetFragment);
        }
        //提交事务
        transaction.commit();
        currentIndex = index;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //处理底部左侧按钮的点击事件
           case R.id.iv_main_left:
                switchFragment(LEFTFRAGMENT);
                break;
            //处理底部右侧按钮的点击事件
            case R.id.iv_main_right:
                switchFragment(RIGHTFRAGMENT);
                break;
            //处理底部中间按钮的点击事件
            case R.id.iv_main_center:
                GetLiving.skipLiving(this);
                break;
        }

    }

}
